package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;

    /**
     * 分页查询菜品
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult dishPageQuery(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<DishVO> page = dishMapper.dishPageQuery(dto);
        long total = page.getTotal();
        List<DishVO> result = page.getResult();
        return new PageResult<>(total, result);
    }

    /**
     * 新增菜品
     *
     * @param dto
     */
    @Override
    public void saveDish(DishDTO dto) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        dish.setStatus(StatusConstant.DISABLE);
        dishMapper.saveDish(dish);

        Long id = dish.getId();
        List<DishFlavor> dishFlavors = dto.getFlavors();
        if (dishFlavors != null && dishFlavors.size() > 0) {
            dishFlavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });

            dishFlavorMapper.dishFlavorList(dishFlavors);
        }
    }

    /**
     * 删除菜品
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteDish(List<Long> ids) {
        //启售中的商品不能删除
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (Objects.equals(dish.getStatus(), StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //套餐中的餐品不能删除
        List<SetmealDish> setmealDishes = setMealDishMapper.getByDishId(ids);

        if (setmealDishes != null && setmealDishes.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品表中的菜品数据

        dishMapper.deleteDish(ids);
        dishFlavorMapper.deletedishFlavor(ids);
    }

    @Transactional
    @Override
    public void updataDish(DishDTO dto) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        dishMapper.updataDish(dish);
        Long id = dish.getId();


        if (dto.getFlavors() != null && !dto.getFlavors().isEmpty()) {
            dishFlavorMapper.deletedishFlavor(Collections.singletonList(id));
            List<DishFlavor> dishFlavorList = dto.getFlavors();
            for (DishFlavor dishFlavor : dishFlavorList) {
                dishFlavor.setDishId(id);
            }
            dishFlavorMapper.dishFlavorList(dishFlavorList);

        }


    }

    @Override
    public DishVO getById(Integer id) {
        Dish dish = dishMapper.getDishById(id);

        List<DishFlavor> dishFlavor = dishFlavorMapper.getDishById(id);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavor);

        return dishVO;
    }

    @Override
    public void status(Integer status, Integer id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(Long.valueOf(id));

        dishMapper.updataDish(dish);
    }

    @Override
    public List<DishVO> getDishList() {
        List<DishVO> dishVOList = dishFlavorMapper.getDishList();
        return dishVOList;

    }
}
